// Change product
function changeSupplies(partner){
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}else{
		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	if(partner===''){
		//Reset
		var limit = $('#order_item_wrap .supplies_dropdown').length;
		for(var i=0 ; i<limit ; i++){
			document.getElementsByClassName("supplies_dropdown")[i].innerHTML = "<select name='supplies[]' style='width: 100%;'><option value=''>----SELECT----</option></select>";		
		}
	}else{
		xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				//var result_json = xmlhttp.responseText;
				//var result_array = Json.parse(result_json);
				var limit = $('#order_item_wrap .supplies_dropdown').length;
				for(var i = 0 ; i<limit;i++){
					//document.getElementById("supplies_dropdown").innerHTML = xmlhttp.responseText;
					//document.getElementById("supplies_dropdown").innerHTML = xmlhttp.responseText;
					document.getElementsByClassName("supplies_dropdown")[i].innerHTML = xmlhttp.responseText;
					
				}
    		}
  		}
		xmlhttp.open("GET","helper/getsupplies.php?partner="+partner,true);
		xmlhttp.send();
	}
}

// Change product
function changePartner(partner){
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}else{
		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	if(partner===''){
		//Reset
		document.document.getElementById("supplies_dropdown").innerHTML = "<select name='supplies' style='width: 90%;'><option value=''>----SELECT----</option></select>";		
	}else{
		xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById("supplies_dropdown").innerHTML = xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","helper/getsupplies2.php?partner="+partner,true);
		xmlhttp.send();
	}
}

// Add field
function getSum(){
	var cost =document.getElementsByName("cost[]");
	//alert(cost.length);
	var sum = 0;
	for(var i=0;i<cost.length;i++){
		if(cost[i].value != ''){
			sum += parseInt(cost[i].value, 10);
		}
	}
	document.getElementById("estimated_total_cost").innerHTML = sum;
}

// get item list
function getOrderItem(field_id,order_id){
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}else{
		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	if(order_id===''){
		//Reset
		var str = "order_item_";
		var div_id = str.concat(field_id);
		document.getElementById(div_id).innerHTML = "<select name='order_item[]' style='width: 100%;'><option value=''>----SELECT----</option></select>";		
	}else{
		xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				
				var str = "order_item_";
				var div_id = str.concat(field_id);
				document.getElementById(div_id).innerHTML = xmlhttp.responseText;
    		}
  		}

		xmlhttp.open("GET","helper/getorderitem.php?invoice_id="+order_id,true);
		xmlhttp.send();
	}
}

// get sales batch list
// get item list
function getProductBatch(field_id,product_id){
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}else{
		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	if(product_id===''){
		//Reset
		var div_id = "product_batch_"+field_id;
		document.getElementById(div_id).innerHTML = "<select name='product_batch[]' style='width: 100%;'><option value=''>----SELECT----</option></select>";
	}else{
		xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				var str = "product_batch_";
				var div_id = str.concat(field_id);
				document.getElementById(div_id).innerHTML = xmlhttp.responseText;
    		}
  		}

		xmlhttp.open("GET","helper/getproductbatch.php?product_id="+product_id,true);

		xmlhttp.send();
	}
}

function updatePrice(field_id){
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}else{
		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	//get user input
	var product_div = "product_"+field_id;
	var product_id = document.getElementById(product_div).value;
	var cal_quantity = "cal_quantity_"+field_id;
	var product_quantity = document.getElementById(cal_quantity).value;
	
	if(product_id!=='' && product_quantity!==''){
		xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				var cal_single = "cal_single_"+field_id;
				var cal_sub_total = "cal_sub_total_"+field_id;
				
				document.getElementById(cal_single).innerHTML = xmlhttp.responseText;
				document.getElementById(cal_sub_total).innerHTML = xmlhttp.responseText * product_quantity;
    		}
  		}

		xmlhttp.open("GET","helper/getprice.php?product_id="+product_id,true);

		xmlhttp.send();
	}else{
		//Reset
		var cal_single = "cal_single_"+field_id;
		var cal_sub_total = "cal_sub_total_"+field_id;
		document.getElementById(cal_single).innerHTML = "";
		document.getElementById(cal_sub_total).innerHTML = "";
	}
}

function updateSales(num){
	for(var i=0;i<num;i++){
		updatePrice(i);	
	}
	
	var total = 0;
	for(var i=0;i<num;i++){
		field_id = "cal_sub_total_" + i;
		value = document.getElementById(field_id).innerHTML;
		if(value !== ''){
			subtotal = parseInt(value);
			total = total + subtotal;
		}	
	}
	document.getElementById("cal_total").innerHTML = total;
}